package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.dto.BiddToPscDto;
import com.project.entity.BiddEntity;
import com.project.entity.BiddSupplierEntity;
import com.project.entity.PromotionEntity;
import com.project.entity.SupplierEntity;
import com.project.mapper.IBiddMapper;
import com.project.mapper.IBiddSupplierMapper;
import com.project.mapper.IPromotionMapper;
import com.project.mapper.ISupplierMapper;
import com.project.service.IBiddService;
import com.project.service.IPromotionService;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Transactional
public class BiddServiceImpl implements IBiddService {

    @Autowired
    private IBiddMapper biddMapper;

    @Autowired
    private IBiddSupplierMapper biddSupplierMapper;

    @Autowired
    private ISupplierMapper supplierMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     *查找招标书状态为已发布招标
     */
    @Override
    public IPage<BiddEntity> findByItem(int pageNO,int pageSize,int status) {
        QueryWrapper<BiddEntity> qw = new QueryWrapper<>();
        qw.eq("b_status",status);
        return biddMapper.selectPage(new Page<>(pageNO,pageSize),qw);
    }

    @Override
    public BiddEntity findById(String biddRef) {
        QueryWrapper<BiddEntity> qw = new QueryWrapper<>();
        qw.eq("b_biddRef",biddRef);
        return biddMapper.selectOne(qw);//查找招标书

    }

    /**
     * 采购申请审核后添加招标书
     * @param biddEntity
     */
    @Override
    public void addBidd(BiddEntity biddEntity) {
            biddMapper.insert(biddEntity);
    }



    /**
     * 招标书发送
     */
    @Override
    public void release(String inviteId, long[] supplierRefArray, String biddTitle) {
        QueryWrapper<BiddEntity> qw = new QueryWrapper<>();
        qw.eq("b_biddRef",inviteId);
        BiddEntity biddEntity = biddMapper.selectOne(qw);
        biddEntity.setTitle(biddTitle);
        biddEntity.setDateTime(LocalDateTime.now());
        biddEntity.setStatus(1);
        //--------------------------------发送到mq
        BiddToPscDto biddToPscDto =ConvertBeanUtil.copy(biddEntity, BiddToPscDto.class);
        biddToPscDto.setSupplierRefArray(supplierRefArray);
        biddToPscDto.setBiddTitle(biddTitle);

        //Java对象转化为JSON对象
        String jsonObject =  JSONObject.toJSONString(biddToPscDto);
        rabbitTemplate.convertAndSend("topicExchangeTwo","rabbit.BiddSender",jsonObject);


        biddMapper.updateById(biddEntity);//保存到记录
        //保存到标书对应的多个供应商
        BiddSupplierEntity biddSupplierEntity = new BiddSupplierEntity();
        biddSupplierEntity.setBiddRef(inviteId);

        for (long supplierRef : supplierRefArray) {
            biddSupplierEntity.setBiddSupplierId(null);
            QueryWrapper<SupplierEntity> qw1 = new QueryWrapper<>();
            qw1.eq("s_supplierRef",supplierRef);
            SupplierEntity supplierEntity = supplierMapper.selectOne(qw1);
            biddSupplierEntity.setSupplierName(supplierEntity.getSupplierName());
            biddSupplierEntity.setSupplierRef(supplierRef);
            biddSupplierMapper.insert(biddSupplierEntity);
        }


    }


}
